<?php

class Sales {

    private $id;
    private $salesDate;
    private $customer;
    private $salesNumber;
    private $items = array();

    public function setId($_id) {
        $this->id = $_id;
    }

    public function getId() {
        return $this->id;
    }

    public function setSalesDate($_date) {
        $this->salesDate = $_date;
    }

    public function getSalesDate() {
        return $this->salesDate;
    }

    public function setCustomer($_customer) {
        if ($_customer instanceof Customer) {
            $this->customer = $_customer;
        }
    }

    public function getCustomer() {
        return $this->customer;
    }

    public function setSalesNumber($_num) {
        $this->salesNumber = $_num;
    }

    public function getSalesNumber() {
        return $this->salesNumber;
    }

    public function setItems($_items) {
        if (is_array($_items)) {
            $this->items = $_items;
        }
    }

    public function getItems() {
        return $this->items;
    }

    public function addToItems($_item) {
        if ($_item instanceof SalesItem) {
            array_push($this->items, $_item);
        }
    }

    public function save() {
        $customer_id = $this->customer->getId();
        mysql_query("insert into sales(id,sales_number,sales_date,customer_id) 
             values('$this->id','$this->salesNumber','$this->salesDate',$customer_id)");

        foreach ($this->items as $item) {
            $product_id = $item->getProduct()->getId();
            $qty = $item->getQty();
            $price = $item->getPrice();
            mysql_query("insert into product_has_sales(product_id,sales_id,qty,price) 
                values($product_id,$this->id,$qty,$price)");
        }
    }

}

?>
